using System;
using Microsoft.WindowsAzure.StorageClient;

namespace AzureContrib.WindowsAzure.StorageClient
{
	/// <summary>
	/// Extensions on <see cref="ICloudQueue"/>
	/// </summary>
	public static class CloudQueueExtensions
	{
		/// <summary>
		/// Call <see cref="ICloudQueue.GetMessage"/> with a <see cref="TimeSpan.FromMinutes"/> based on <paramref name="minutes"/>
		/// </summary>
		/// <param name="cloudQueue">The queue</param>
		/// <param name="minutes">The number of minutes to get the message for.</param>
		/// <returns><see cref="CloudQueueMessage"/></returns>
		public static CloudQueueMessage GetMessageForMinutes(this ICloudQueue cloudQueue, double minutes)
		{
			return cloudQueue.GetMessage(TimeSpan.FromMinutes(minutes));
		}

		/// <summary>
		/// Call <see cref="ICloudQueue.GetMessage"/> with a <see cref="TimeSpan.FromMinutes"/> based on <paramref name="seconds"/>
		/// </summary>
		/// <param name="cloudQueue">The queue</param>
		/// <param name="seconds">The number of seconds to get the message for.</param>
		/// <returns><see cref="CloudQueueMessage"/></returns>
		public static CloudQueueMessage GetMessageForSeconds(this ICloudQueue cloudQueue, double seconds)
		{
			return cloudQueue.GetMessage(TimeSpan.FromSeconds(seconds));
		}

		/// <summary>
		/// Add an instance of an object to a <see cref="ICloudQueue"/> by serializing it.
		/// </summary>
		public static void AddMessage(this ICloudQueue cloudQueue, object o)
		{
			cloudQueue.AddMessage(o.AsMessage());
		}

		/// <summary>
		/// Get a <see cref="CloudQueueMessage{T}"/> from the queue
		/// </summary>
		/// <typeparam name="T">The type of message serialized into the queue</typeparam>
		/// <param name="cloudQueue">The queue get from.</param>
		/// <param name="visibilityTimeout"><see cref="CloudQueue.GetMessage()"/></param>
		/// <returns>The message with it's payload</returns>
		public static CloudQueueMessage<T> GetMessage<T>(this ICloudQueue cloudQueue, TimeSpan visibilityTimeout) where T : class
		{
			return cloudQueue.GetMessage(visibilityTimeout);
		}

		/// <summary>
		/// Get a <see cref="CloudQueueMessage{T}"/> from the queue
		/// </summary>
		/// <typeparam name="T">The type of message serialized into the queue</typeparam>
		/// <param name="cloudQueue">The queue get from.</param>
		/// <param name="minutes">Minutes to work on the message</param>
		/// <returns>The message with it's payload</returns>
		public static CloudQueueMessage<T> GetMessageForMinutes<T>(this ICloudQueue cloudQueue, double minutes) where T : class
		{
			return GetMessage<T>(cloudQueue, TimeSpan.FromMinutes(minutes));
		}

		/// <summary>
		/// Get a <see cref="CloudQueueMessage{T}"/> from the queue
		/// </summary>
		/// <typeparam name="T">The type of message serialized into the queue</typeparam>
		/// <param name="cloudQueue">The queue get from.</param>
		/// <param name="seconds">Seconds to work on the message</param>
		/// <returns>The message with it's payload</returns>
		public static CloudQueueMessage<T> GetMessageForSeconds<T>(this ICloudQueue cloudQueue, double seconds) where T : class
		{
			return GetMessage<T>(cloudQueue, TimeSpan.FromMinutes(seconds));
		}
	}
}